package com.zeroes.cons;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;

public class TouchCentral {
	
	public static final int NONE = 0;
	public static final int SINGLE = 1;
	public static final int DRAG = 2;
	public static final int GESTURE = 3;
	public static final int SINGLE_UP = 4;
	
	private float mPreviousX;
	private float mPreviousY;
	
	float oldDist = 100.0f;
	float newDist;
	
	public int mode=0;
	
	private static TouchCentral _touchmanager = new TouchCentral();
	
	public class Touch{
		public int mode ;
		public float lastX;
		public float lastY;
		public float upX;
		public float upY;
		public float dx;
		public float dy;
	}
	
	public Touch lastTouch =new Touch(); 
	
	public TouchCentral(){
		
		
	}
	   public static TouchCentral AskMe() {
//	        if (_sharedDirector != null)
	            return _touchmanager;
	   }
	   
	   public void processEvent(MotionEvent e){
		   float x = e.getX();
		   float y = e.getY();
		   
			switch (e.getAction()) {
			case MotionEvent.ACTION_DOWN:			// one touch: drag
				Log.d("ConstructoidActivity", "mode=SINGLE "+x+" , "+y );
				mode = SINGLE;
				mPreviousX = x;
				mPreviousY = y;
				lastTouch.lastX=x;
				lastTouch.lastY=y;
				lastTouch.mode=mode;
				break;
			case MotionEvent.ACTION_POINTER_DOWN:	// two touches: zoom
				Log.d("ConstructoidActivity", "mode=GESTURE" );
				oldDist = spacing(e);
				if (oldDist > 10.0f) {
					mode = GESTURE; // zoom
				}
				break;
			case MotionEvent.ACTION_UP:		// no mode
				mode = SINGLE_UP;
				lastTouch.upX=x;
				lastTouch.upY=y;
				lastTouch.mode=mode;
				
				Log.d("ConstructoidActivity", "mode=UP" );
				oldDist = 100.0f;
				break;
			case MotionEvent.ACTION_POINTER_UP:		// no mode
				mode = NONE;
				Log.d("ConstructoidActivity", "mode=UPGesture" );
				oldDist = 100.0f;
				break;
			case MotionEvent.ACTION_MOVE:	
				// rotation
			
				if (e.getPointerCount() > 1 && mode == GESTURE) {
					newDist = spacing(e);
					Log.d("SPACING: ", "OldDist: " + oldDist + ", NewDist: " + newDist);
					if (newDist > 10.0f) {
	

						oldDist = newDist;
					}
				}
				else if (mode == SINGLE/*||mode== DRAG*/){
					float dx = x - mPreviousX;
					float dy = y - mPreviousY;
					//mode=DRAG;
					if(Math.abs(dx)>15||Math.abs(dy)>15){
					lastTouch.dx=dx;
					lastTouch.dy=dy;
					lastTouch.mode=this.DRAG;
					Log.d("ConstructoidActivity", "mode=DRAG "+dx+" , "+dy );
					}
					}
				break;
			}

		   
	   }
	   
		private float spacing(MotionEvent event) {
			float x = event.getX(0) - event.getX(1);
			float y = event.getY(0) - event.getY(1);
			return FloatMath.sqrt(x * x + y * y);
		}

}
